﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using hello.csharp.common.Enums;
using hello.csharp.common.Interfaces.Logics;
using hello.csharp.helper;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

/**
 * 相关加密部分参考下面的链接：
 * http://blog.jobbole.com/61872/ 加盐密码哈希：如何正确使用
 * https://blog.coderzh.com/2016/01/03/security-design/ 设计安全的账号系统的正确姿势
 */

namespace hello.csharp.controller.authority
{
    /// <summary>
    /// 账户管理
    /// </summary>
    [CustomRoute]
    public class AccountController : BaseApiController
    {
        private readonly IAccountRegisterLogic _registerLogic;

        public AccountController(IAccountRegisterLogic registerLogic)
        {
            _registerLogic = registerLogic;
        }

        /// <summary>
        /// 来点胡椒面
        /// </summary>
        [HttpGet]
        public IActionResult GetPepperValue()
        {
            return Ok(new
            {
                pepper = RandomHelper.GetRandomString(64)
            });
        }

        /// <summary>
        /// 注册
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Register([FromBody] dynamic obj)
        {
            string nickname = obj.nickname;//昵称
            string email = obj.email;//用户Email
            string password = obj.password;//md5后的密码值
            string mobile = obj.mobile;//md5后的密码值
            bool result = await _registerLogic.RegisterAsync(nickname, email, mobile, password);
            return Ok();
        }

        /// <summary>
        /// Email确认
        /// </summary>
        /// <returns></returns>
        [HttpGet("{email}/{value}")]
        public async Task<IActionResult> ConfirmEmail(string email, string value)
        {
            bool result = await _registerLogic.SendUsableEmailAsync(email, value);
            return Ok();
        }

        /// <summary>
        /// 手机号码确认
        /// </summary>
        /// <returns></returns>
        [HttpGet("{mobile}/{value}")]
        public async Task<IActionResult> ConfirmMobile(string mobile, string value)
        {
            bool result = await _registerLogic.SendUsableMobileAsync(mobile, value);
            return Ok();
        }

        /// <summary>
        /// 校验昵称是否已经存在
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> IsOnlyNickname(string nickname)
        {
            bool result = await _registerLogic.IsOnlyAsync(OnlyItem.Nickname, nickname);
            return Ok();
        }

        /// <summary>
        /// 校验Email是否已经存在
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> IsOnlyEmail(string email)
        {
            bool result = await _registerLogic.IsOnlyAsync(OnlyItem.Email, email);
            return Ok();
        }

        /// <summary>
        /// 校验手机是否存在
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> IsOnlyMobile(string mobile)
        {
            bool result = await _registerLogic.IsOnlyAsync(OnlyItem.Mobile, mobile);
            return Ok();
        }
    }
}